home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / SAT 2.1.2 / HeartQuest sample ƒ / CenterStuff.p next >
Encoding:
Text File  |  1993-07-31  |  4.2 KB  |  147 lines  |  [TEXT/PJMM]

  1. unit CenterStuff;
  2.  
  3. interface
  4.  
  5.     procedure CenterDialog (id: integer);
  6.     procedure CenterPDialog (id: integer; var corner: Point);
  7.     procedure CenterRect (var r: Rect);
  8.     function DoAlert (kind, alertID: integer; filterProc: ProcPtr): integer;
  9.  
  10. implementation
  11.  
  12. {Based on code from the following post:}
  13.  
  14. {From: Michael_Hecht@mac.sas.com (Michael Hecht)}
  15. {Newsgroups: comp.sys.mac.programmer}
  16. {Subject: Re: How to tell if a window has a title bar?}
  17. {Date: 26 Mar 92 20:03:05 GMT}
  18. {Here's how I compute the height of a window's title bar: …}
  19.  
  20.     function GetTitleHeight (theWindow: WindowPtr): integer;
  21.         var
  22.             windRect: Rect;
  23.             bias: integer;
  24.     begin
  25.     { Convert window's portRect to global coordinates }
  26.         windRect := theWindow^.portRect;
  27.         LocalToGlobal(windRect.topLeft);
  28.         LocalToGlobal(windRect.botRight);
  29.  
  30.     { Calculate the height of the window's title bar }
  31.         bias := windRect.top - 1 - WindowPeek(theWindow)^.strucRgn^^.rgnBBox.top;
  32.         windRect.top := windRect.top - bias;
  33. { What is the result? windRect? [I HAVN'T PORTED THIS TO WORKING CONDITION! /Ingemar]}
  34.     end;
  35.  
  36. {> (Or, just in case I've lost the woods for the trees, how do I accurately}
  37. {> center my windows?)}
  38.  
  39. {Here's what I do:}
  40.  
  41. { CenterRect:    Center a rectangle on the main screen }
  42.     procedure CenterRect (var r: Rect);
  43.         var
  44.             delta, screenTop: integer;
  45.     begin
  46.     { Center it horizontally }
  47.         delta := r.right - r.left;
  48.         r.left := BSR(screenBits.bounds.right - delta, 1);
  49.         r.right := r.left + delta;
  50.  
  51.     { Determine top of screen }
  52.         screenTop := 22; {GetMBarHeight ( ); { I'm cheating a bit here - GetMBarHeight would be better. /Ingemar }
  53.  
  54.     { Place it in upper third of screen }
  55.         delta := r.bottom - r.top;
  56.         r.top := (screenBits.bounds.bottom - screenTop - delta) div 3 + screenTop;
  57.         r.bottom := r.top + delta;
  58.     end;
  59.  
  60.     procedure CenterDialog (id: integer);
  61.         var
  62.             theDLOG: DialogTHndl;
  63.             bounds: Rect;
  64.     begin
  65.     { Get the DLOG resource }
  66.         theDLOG := DialogTHndl(GetResource('DLOG', id));
  67.         if (theDLOG = nil) then
  68.             exit(CenterDialog);
  69.  
  70.     { Center it within screenBits }
  71.         bounds := theDLOG^^.boundsRect;
  72.         CenterRect(bounds);
  73.         theDLOG^^.boundsRect := bounds;
  74.     end;
  75.  
  76.     procedure CenterPDialog (id: integer; var corner: Point);
  77.         var
  78.             theDLOG: DialogTHndl;
  79.             bounds: Rect;
  80.     begin
  81.     { Get the DLOG resource }
  82.         theDLOG := DialogTHndl(GetResource('DLOG', id));
  83.         if (theDLOG = nil) then
  84.             exit(CenterPDialog);
  85.  
  86.     { Center it within screenBits }
  87.         bounds := theDLOG^^.boundsRect;
  88.         CenterRect(bounds);
  89.         theDLOG^^.boundsRect := bounds;
  90.  
  91.         if (longint(corner) <> 0) then
  92.             corner := bounds.topLeft;
  93.     end;
  94.  
  95.     procedure CenterAlert (id: integer);
  96.         var
  97.             theALRT: AlertTHndl;
  98.             bounds: Rect;
  99.     begin
  100.     { Get the ALRT resource }
  101.         theALRT := AlertTHndl(GetResource('ALRT', id));
  102.         if (theALRT = nil) then
  103.             exit(CenterAlert);
  104.  
  105.     { Center it within screenBits }
  106.         bounds := theALRT^^.boundsRect;
  107.         CenterRect(bounds);
  108.         theALRT^^.boundsRect := bounds;
  109.     end;
  110.  
  111.     function DoAlert (kind, alertID: integer; filterProc: ProcPtr): integer;
  112.         var
  113.             item: integer;
  114.     begin
  115.         CenterAlert(alertID);
  116.         InitCursor;        { Equivalent to SetCursor(&arrow);}
  117.         case kind of
  118.             stopIcon: 
  119.                 item := StopAlert(alertID, filterProc);
  120.             noteIcon: 
  121.                 item := NoteAlert(alertID, filterProc);
  122.             cautionIcon: 
  123.                 item := CautionAlert(alertID, filterProc);
  124.             otherwise
  125.                 item := Alert(alertID, filterProc);
  126.         end;
  127.         DoAlert := item;
  128.     end;
  129.  
  130.  
  131. {More from the original posting (but don't blame him for errors I've introduced!):}
  132.  
  133. {For dialogs, I call CenterDialog. It will optionally hand me back the top,left}
  134. {coordinate (handy for calls to SFGetFile, etc.). For alerts, I just call}
  135. {DoAlert, which will center it for me. Note that it adjusts for the menu bar}
  136. {but not for the title bar. This is because my method is used BEFORE calling}
  137. {GetNewDialog, as it tweaks the in-memory copy of the DLOG resource. There's}
  138. {no structure region to peek at at that time. If you're centering the window}
  139. {after its created, you could use the bounding box of the window's structure}
  140. {region, rather than its portRect. That would take care of the title bar as}
  141. {well as anything else.}
  142.  
  143. {Have fun!}
  144. {Michael P. Hecht                 | Internet:  Michael_Hecht@mac.sas.com}
  145. {SAS Institute Inc.; Cary, NC USA | AppleLink: SAS.HECHT}
  146.  
  147. end.